package com.bestcem.xm.qdes.controller;

import com.bestcem.xm.common.core.constant.ReqErrorCodeConstants;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.qdes.controller.param.UpdateQdesTagrefParam;
import com.bestcem.xm.qdes.entity.QdesTagRef;
import com.bestcem.xm.qdes.service.QdesTagRefService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "问卷标签")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qdes/projectstag")
public class QdesTagrefController {
    private @NonNull QdesTagRefService qdesTagrefService;

    /**
     * 获取问卷标签
     *
     * @return
     */
    @ApiOperation(value = "获取问卷标签")
    @GetMapping("/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<List<QdesTagRef>> getQdesTagref(@PathVariable String id) {
        return XmResultJson.success(qdesTagrefService.getQdesTagref(id));
    }

    /**
     * 设置问卷标签
     *
     * @return
     */
    @ApiOperation(value = "设置问卷标签")
    @PutMapping("/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<List<QdesTagRef>> updateQdesTagref(@PathVariable String id, @RequestBody UpdateQdesTagrefParam param) {
        List<UpdateQdesTagrefParam.TagData> labels = param.getLabels();
        if (CollectionUtils.isEmpty(labels)) {
            return XmResultJson.fail(ReqErrorCodeConstants.PARAM_LOST, "labels must not be empty");
        }
        qdesTagrefService.updateQdesTagref(id, labels);
        return XmResultJson.success();
    }
}
